import {createDraw} from './Draw'
import {reactive,ref } from 'vue';
import {LineString} from './LineString'
let draw = reactive({})
let resultName   // 这里提前声明resultName
let array = ref([])

class addAounce{

  // 1, 发布公告事件回调函数
  static announce(map,source,docLayer,attrs){
    // source.clear()
    draw.value = createDraw({
      type:'LineString',
      source,
      callback:(e)=>addAounce.handleDraw(e,docLayer,map,source,attrs)
    })
    map.addInteraction(draw.value)
    // 绘制完一条线之后移除画笔
    draw.value.on('drawend',function(){map.removeInteraction(draw.value)})
  }

  // 2, 激活画笔的回调----将绘制好的线添加到线图层docLayer
  static handleDraw(e,docLayer,map,source,attrs){
    let position = e.feature.getGeometry().getCoordinates()
    const attr = [{key:'name',value:'铁路',type:'string'}]
    const service = {docName:'LineLayer',layerId:0}
    LineString.addLine({position,attr,service,docLayer})
    //3,缓冲区分析
    addAounce.singleBuffAnalysis(map,source,attrs) 
  }

  // 3,单圈缓冲区分析
  static singleBuffAnalysis(map,source,attrs) {
    // 3.1、创建缓冲区分析对象
    // 实例化ClassBufferBySingleRing类
    var clsBufBySR = new Zondy.Service.ClassBufferBySingleRing({
        ip: 'localhost',
        port: '6163',
        //经纬度坐标系下,缓冲区的半径单位为度
        leftRad: 0.002,
        rightRad: 0.002,
        //不允许根据属性字段设置缓冲区半径
        isByAtt: false
    })
    resultName = 'gdbp://MapGISLocalPlus/0808/sfcls/LineLayer' + Date.now()
    //3.2、设置参数
    //设置源图层
    clsBufBySR.srcInfo = 'gdbp://MapGISLocalPlus/0808/sfcls/LineLayer'
    //设置目标图层
    clsBufBySR.desInfo = resultName;
    //3.3、 执行分析
    clsBufBySR.execute(
        (result)=>addAounce.onBuffSuccess(result,map,source,attrs),
        'post',
        false,
        'json'
    )
  }


  //4,缓冲区分析的回调---------执行剪裁
 static onBuffSuccess(result,map,source,attrs){
  source.clear()
    if(result.succeed){
      var clspath = result.results[0].Value
      //实例化ClipByLayer类
      var clipParam = new Zondy.Service.ClipByLayer({
          ip: 'localhost',
          port: 6163,
          //源简单要素类的URL
          srcInfo1: "gdbp://MapGisLocal/wuhan/sfcls/居民区",
          //裁剪框简单要素类的URL
          srcInfo2: clspath,
          //设置结果URL
          desInfo: "gdbp://MapGISLocalPlus/0808/sfcls/LineLayer" +  Date.now(),
          infoOptType: 0
      });
       //调用基类的execute方法，执行图层裁剪分析。AnalysisSuccess为结果回调函数
       clipParam.execute((result)=>addAounce.onClipSuccess(result,map,attrs), "post", false, "json");
    }
  }


  // 5, 剪裁的回调----(1)添加剪裁的图层给map,(2)并在igserver中删除线图层中的线
  static onClipSuccess(result,map,attrs){
    const newpath = result.results[0].Value
    const resultLayer = new Zondy.Map.GdbpLayer('', [newpath], {
      ip: 'localhost',
      port: 6163
    })
    map.addLayer(resultLayer)

    // 5.1查询剪裁获得的图形中的属性，尤其要获得所有的小区名称和电话
    addAounce.queryLayer({url:newpath,callback:(e)=>addAounce.queryAttrSuccess(e,attrs)})
    // 5.2查询oid,执行删除
    addAounce.queryLayer({ url:"gdbp://MapGISLocalPlus/0808/sfcls/LineLayer", callback:addAounce.queryLayerSuccess })
  }

  static queryAttrSuccess(e,attrs){
    let arr = []
    e.SFEleArray.forEach( item => {
      arr.push({name:item.AttValue[4],tel:item.AttValue[5]})
    })
    attrs.value = arr
  }
  // 6, 查询oid，执行删除
  static queryLayerSuccess(e){
    e.SFEleArray.forEach(item=>{
    array.value.push(item.FID)
    })
    var deleteService = new Zondy.Service.EditDocFeature("LineLayer", 0, { 
      ip: "localhost", 
      port: "6163"    //访问IGServer的端口号，.net版为6163，Java版为8089
    });
    deleteService.deletes(array.value);    
  }

  // 根据url对mapgis中图层的属性进行查询
  static queryLayer({url,callback}) {
    //初始化查询结构对象，设置查询结构包含几何信息
    var queryStruct = new Zondy.Service.QueryFeatureStruct();
    //是否包含几何图形信息
    queryStruct.IncludeGeometry = false;
    //是否包含属性信息
    queryStruct.IncludeAttribute = true;
    //是否包含图形显示参数
    queryStruct.IncludeWebGraphic = false;
    //var objectIds = "10,104,185,238";
    //实例化查询参数对象
    var queryParam = new Zondy.Service.QueryByLayerParameter(url, {
        resultFormat: "json",
        struct: queryStruct
    });
    queryParam.recordNumber = 1000
    //实例化地图文档查询服务对象
    var queryService = new Zondy.Service.QueryLayerFeature(queryParam, {
        ip: "localhost",
        port: "6163"    //访问IGServer的端口号，.net版为6163，Java版为8089
    });
    //执行查询操作，querySuccess为查询回调函数
    queryService.query(callback);
  }

}

export default addAounce